﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using swaganomics.Windows;
using Microsoft.Win32;
using System.Web.Script.Serialization;
using swaganomics.Metro.Dialogs;
using swaganomics;

namespace swaganomics.Backend
{
    public class Settings
    {
        public static void LoadSettings(bool applyThemeAswell = false)
        {
            // Declare Registry
            RegistryKey keyApp = Registry.CurrentUser.CreateSubKey("Software\\YRS\\Swaganomics\\appSettings\\");
            // Create a JSON Seralizer
            JavaScriptSerializer jss = new JavaScriptSerializer();

            applicationAccent = (Accents)keyApp.GetValue("accent", 0);
            if (applyThemeAswell)
                ApplyAccent();

            applicationXDKNameIP = keyApp.GetValue("XDKNameIP", "192.168.1.0").ToString();
            applicationRecents = jss.Deserialize<List<RecentFileEntry>>(keyApp.GetValue("RecentFiles", "").ToString());

            startpageShowOnLoad = Convert.ToBoolean(keyApp.GetValue("ShowStartPageOnLoad", true));
            startpageHideOnLaunch = Convert.ToBoolean(keyApp.GetValue("HideStartPageOnLaunch", false));
            startpageShowRecentsMap = Convert.ToBoolean(keyApp.GetValue("ShowRecentsMap", true));
            startpageShowRecentsBLF = Convert.ToBoolean(keyApp.GetValue("ShowRecentsBLF", true));
            startpageShowRecentsMapInfo = Convert.ToBoolean(keyApp.GetValue("ShowRecentsMapInfo", true));

            halomapTagSort = (TagSort)keyApp.GetValue("TagSorting", 0);
            halomapRelativePluginRoot = keyApp.GetValue("PluginRoot", VariousFunctions.ApplicationLocation() + @"Plugins\").ToString();
            halomapShowEmptyClasses = Convert.ToBoolean(keyApp.GetValue("ShowEmptyClasses", false));
            halomapLastSelectedMetaEditor = (LastMetaEditorType)keyApp.GetValue("LastSelectedMetaEditor", 0);
        }
        public static void UpdateSettings(bool applyThemeAswell = false)
        {
            // Declare Registry
            RegistryKey keyApp = Registry.CurrentUser.CreateSubKey("Software\\Xeraxic\\swaganomics\\appSettings\\");
            // Create a JSON Seralizer
            JavaScriptSerializer jss = new JavaScriptSerializer();

            keyApp.SetValue("accent", (int)applicationAccent);
            if (applyThemeAswell)
                ApplyAccent();

            keyApp.SetValue("XDKNameIP", applicationXDKNameIP);
            keyApp.SetValue("RecentFiles", jss.Serialize(applicationRecents));

            keyApp.SetValue("ShowStartPageOnLoad", startpageShowOnLoad);
            keyApp.SetValue("HideStartPageOnLaunch", startpageHideOnLaunch);
            keyApp.SetValue("ShowRecentsMap", startpageShowRecentsMap);
            keyApp.SetValue("ShowRecentsBLF", startpageShowRecentsBLF);
            keyApp.SetValue("ShowRecentsMapInfo", startpageShowRecentsMapInfo);

            keyApp.SetValue("TagSorting", (int)halomapTagSort);
            keyApp.SetValue("PluginRoot", halomapRelativePluginRoot);

            keyApp.SetValue("ShowEmptyClasses", halomapShowEmptyClasses);
            keyApp.SetValue("LastSelectedMetaEditor", (int)halomapLastSelectedMetaEditor);
        }


        public static void ApplyAccent()
        {
            string theme = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Enum.Parse(typeof(Accents), applicationAccent.ToString()).ToString());
            try
            {
                App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("/Assembly;component/Metro/Themes/" + theme + ".xaml", UriKind.Relative) });
            }
            catch
            {
                App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("/Assembly;component/Metro/Themes/Blue.xaml", UriKind.Relative) });
            }
        }
        public static Accents applicationAccent = Accents.Blue;

        public static string applicationXDKNameIP = "";
        public static List<RecentFileEntry> applicationRecents = new List<RecentFileEntry>();

        public static bool startpageShowOnLoad = true;
        public static bool startpageHideOnLaunch = false;
        public static bool startpageShowRecentsMap = true;
        public static bool startpageShowRecentsBLF = true;
        public static bool startpageShowRecentsMapInfo = true;

        public static TagSort halomapTagSort = TagSort.TagClass;
        public static string halomapRelativePluginRoot = VariousFunctions.ApplicationLocation() + @"Plugins/";
        public static bool halomapShowEmptyClasses = false;
        public static LastMetaEditorType halomapLastSelectedMetaEditor = LastMetaEditorType.Reflex;

        public static HomeWindow homeWindow = null;

        public enum Accents
        {
            Blue,
            Purple,
            Orange,
            Green
        }
        public enum RecentFileType
        {
            Cache,
            MapInfo,
            BLF
        }
        public enum TagSort
        {
            TagClass,
            ObjectHierarchy,
            PathHierarchy
        }
        public enum LastMetaEditorType
        {
            Reflex,
            Ident,
            String,
            MetaEditor
        }
        public class RecentFileEntry
        {
            public string FileName { get; set; }
            public RecentFileType FileType { get; set; }
            public string FileGame { get; set; }
            public string FilePath { get; set; }
        }
    }

    public class TempStorage
    {
        public static MetroMessageBox.MessageBoxResults msgBoxButtonStorage;
    }

    public class RecentFiles
    {
        public static void AddNewEntry(string filename, string filepath, string game, Settings.RecentFileType type)
        {
            Settings.RecentFileEntry alreadyExistsEntry = null;

            if (Settings.applicationRecents == null)
                Settings.applicationRecents = new List<Settings.RecentFileEntry>();

            foreach (Settings.RecentFileEntry entry in Settings.applicationRecents)
                if (entry.FileName == filename || entry.FilePath == filepath)
                    alreadyExistsEntry = entry;

            if (alreadyExistsEntry == null)
            {
                // Add New Entry
                Settings.RecentFileEntry newEntry = new Settings.RecentFileEntry()
                {
                    FileGame = game,
                    FileName = filename,
                    FilePath = filepath,
                    FileType = type
                };
                Settings.applicationRecents.Insert(0, newEntry);
            }
            else
            {
                // Move existing Entry
                Settings.applicationRecents.Remove(alreadyExistsEntry);
                Settings.applicationRecents.Insert(0, alreadyExistsEntry);
            }

            Settings.UpdateSettings();
        }

        public static void RemoveEntry(Settings.RecentFileEntry entry)
        {
            Settings.applicationRecents.Remove(entry);
            Settings.UpdateSettings();
        }
    }
}
